//
//  XCRankViewController.m
//  SoundOfNature
//
//  Created by 梁新昌 on 16/3/10.
//  Copyright © 2016年 梁新昌. All rights reserved.
//

#import "XCRankViewController.h"
#import "XCUrlManger.h"
#import "XCAFNetTool.h"
#import "XCRankModel.h"
#import "XCRankTableViewCell.h"
#import "XCInTheRankViewController.h"
#import "XCInTheRankWebViewController.h"
@interface XCRankViewController ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, retain) UITableView *tableView;
@property (nonatomic, retain) NSMutableArray *dataSource;
@property (nonatomic, retain) UIView *underView;
@property (nonatomic, retain) XCRankModel *rankModel;
@property (nonatomic, retain) NSNumber *ranklist_id;
@property (nonatomic, copy) NSString *link;

@end

@implementation XCRankViewController

- (void)dealloc {
    _tableView.delegate = nil;
    _tableView.dataSource = nil;
    
    [_tableView release];
    [_dataSource release];
    [_underView release];
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"排行榜";
    self.underView = [[UIView alloc] initWithFrame:self.view.bounds];
    [self.view addSubview:self.underView];
//    self.underView.backgroundColor = [UIColor whiteColor];
//
    [self RankNetRequestAction];
    [self tableViewBuildAction];
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"Image-leftBarButton"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] style:UIBarButtonItemStylePlain target:self action:@selector(leftBarButtonItemAction)];
    
}
#pragma mark - RankNetRequest
- (void)RankNetRequestAction {
    [XCAFNetTool GETNetWithUrl:[XCUrlManger RankNetRequest] body:nil headerFile:nil response:XCJSON success:^(id result) {
        self.dataSource = [NSMutableArray arrayWithCapacity:0];
        for (NSDictionary *dic in result) {
            self.rankModel = [[XCRankModel alloc] initWithDataSource:dic];
            [self.dataSource addObject:self.rankModel];
            [self.rankModel release];
        }
//        NSLog(@"%@", self.dataSource);
        
        [self.tableView reloadData];
        
    } failure:^(NSError *error) {
        NSLog(@"网络错误");
    }];
    
}
#pragma mark - tableView 创建
- (void)tableViewBuildAction {
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, wide, high - 70) style:UITableViewStylePlain];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.backgroundColor = [UIColor clearColor];

    [self.underView addSubview:self.tableView];
//    注册重用池
    [self.tableView registerClass:[XCRankTableViewCell class] forCellReuseIdentifier:@"XCRankTableViewCellIdentifier"];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    [_tableView release];
}
// 返回分区
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.dataSource.count;
}
// 返回分区的头视图
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, wide, 21)];
    UILabel *lable = [[UILabel alloc] initWithFrame:CGRectMake(10, 0, wide / 5, 21)];
    XCRankModel *rankModel = self.dataSource[section];
    lable.text = rankModel.name;
    
    [view addSubview:lable];
    
    [lable release];
    view.backgroundColor = [UIColor colorWithWhite:0.902 alpha:1.000];
    return [view autorelease];
    
    
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 21;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    XCRankModel *rankModel = self.dataSource[section];
    return rankModel.refs.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    XCRankTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"XCRankTableViewCellIdentifier"];
    
    XCRankModel *rankModel = self.dataSource[indexPath.section];
    cell.refsModel = rankModel.mutableArray[indexPath.row];
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    XCRankModel *rankModel = self.dataSource[indexPath.section];
    XCRefsModel *refsModel = rankModel.mutableArray[indexPath.row];
    self.ranklist_id = refsModel.ranklist_id;
    self.link = refsModel.link;
    
    if (self.link) {
        XCInTheRankWebViewController *inTheRankWebVC = [[XCInTheRankWebViewController alloc] initWithLink:self.link WithNavigationName:refsModel.title];
        [self.navigationController pushViewController:inTheRankWebVC animated:YES];
        
    } else {
        XCInTheRankViewController *inTheRankVC = [[XCInTheRankViewController alloc] initWithRanklist_id:self.ranklist_id WithName:refsModel.title];
        [self.navigationController pushViewController:inTheRankVC animated:YES];
    }
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
    
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return wide / 3;
}
- (void)leftBarButtonItemAction {
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
}



@end
